﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using Exhibition.RenderTemplete.Controls;
using Exhibition.Common.Helper;
using Exhibition.Service;

namespace Exhibition.Manage
{
    public partial class CommonDataList : BaseAdminPage
    {
        public   string table = string.Empty;
        string isDel = string.Empty;
        string funCode = string.Empty;
        //是否全部数据
        bool isAll = false;
        //是否显示自己的数据
        bool isOwner = false;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["Tb"]))
            {
                table = Request["Tb"];
                isDel = Request["isDel"];
                funCode = Request["FunCode"];
                if (string.IsNullOrEmpty(table))
                {
                    JsHelper.Alert(this, CommFunMsg.GetMessage("s0003", Lan.zh)); 
                    return;
                }
                if (!IsPostBack)
                {

                    grid = RenderGridView.BuildASPXGridView(grid, table,ref isAll,ref isOwner);
                    //表头

                    ltrTbName.Text = grid.Caption;
                    //int Permission = UserPermissions.GetUserPermissionForm(funCode);
                    int Permission = 5;
                    //1 只读，对有权限的模块数据有只读权限
                    //2 修改，对有权限的模块数据有读取/修改权限
                    //3 新建，对有权限的模块数据有读取/新建/修改权限
                    //4 删除，对有权限的模块数据有读取/新建/修改/删除权限
                    //5 转移，包括上述全部权限且增加转移权限
                    switch (Permission)   //权限控制
                    {
                        case 0: return;
                        case 1:
                            btnAdd.Visible = false;
                            btnDelete.Visible = false;
                            btnSelAll.Visible = false;
                            btnUnSelAll.Visible = false;
                            break;
                        case 2:
                            btnAdd.Visible = false;
                            //grid.Columns.Insert(0, RenderGridView.EditColumn(table, grid.KeyFieldName));
                            break;
                        case 3:
                            btnAdd.Visible = true;
                            //grid.Columns.Insert(0, RenderGridView.EditColumn(table, grid.KeyFieldName));
                            btnDelete.Visible = false;
                            break;
                        case 4:
                            btnAdd.Visible = true;
                            //grid.Columns.Insert(0, RenderGridView.EditColumn(table, grid.KeyFieldName));
                            btnDelete.Visible = true;
                            break;
                        case 5:
                            btnAdd.Visible = true;
                           // grid.Columns.Insert(0, RenderGridView.EditColumn(table, grid.KeyFieldName));
                            btnDelete.Visible = true;
                            break;
                        default:
                            btnAdd.Visible = false;
                            btnDelete.Visible = false;
                            break;

                    }


                }
                LoadData();

                RenderGridView.WhereFilds = "";

            }
       
        }

        private void LoadData()
        {
            //RenderGridView.WhereFilds = RenderGridView.GridFilter(grid,   "filter"); 

            string whr = string.Empty;
           
            if (!isAll)
            {
                if (CurrentSite ==null)
                { 
                    JsHelper.Alert(this, CommFunMsg.GetMessage("A0001", Lan.zh));
            
                    return;
                }

                if (catcheSiteID != 0)
                {
                    whr = " and SiteID=" + catcheSiteID;
                }
                else {
                    whr = " and SiteID=" + CurrentSite.Id;
                }
            }
            if (isOwner) {
                whr += " and CreateUserId="+AdminUser.Id;
            }

            whr = whr + RenderGridView.GetWhereFiled("");
            int count = 0;
            int pageSize = pager.ItemsPerPage;
            int pageIndex = pager.PageIndex;
            DataTable dt = CenterService.operatingDataService.GetCommonData(table, pageSize, pageIndex, whr, isDel, out count);
            grid.Caption = "";
            grid.DataSource = dt;
            grid.DataBind();

            pager.ItemCount = count;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect(table.Replace("E_","") + "Detail.aspx");
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            List<object> lisDel = grid.GetSelectedFieldValues(new string[] { grid.KeyFieldName });
            string redUrl = string.Empty;
            foreach (var obj in lisDel)
            {
                try
                {
                    if (Convert.ToInt32(obj) == 1 && table == "E_AdminUser")
                    {
                        JsHelper.Alert(this, CommFunMsg.GetMessage("s0002", Lan.zh));
                        return;
                    }
                    if (!string.IsNullOrEmpty(isDel))
                    {
                        CenterService.operatingDataService.DeleteData(Convert.ToInt32(obj), table);
                       
                    }
                    else {
                        CenterService.operatingDataService.ExecuteNonQuery("DELETE FROM "+table+" WHERE ID="+Convert.ToInt32(obj));
                       
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.Error(CommFunMsg.GetMessage("e0001", Lan.zh) + ex.Message);
                }
            }
            if (!string.IsNullOrEmpty(isDel))
            {
                redUrl = "?TB=" + table + "&isDel=0";
            }
            else {
                redUrl = "?TB=" + table;
            }

            JsHelper.AlertAndRedirect(this, CommFunMsg.GetMessage("a0001", Lan.zh), redUrl);
        }
        protected void btnExport_Click(object sender, EventArgs e)
        {
            if (RenderGridView.GetSelectRows(grid).Count == 0)//如果都不选则导出所有
            {

                gridExport.ExportedRowType = DevExpress.Web.ASPxGridView.Export.GridViewExportedRowType.All;
            }
            if (grid.Columns["ID"] != null)
            {
                grid.Columns["ID"].Visible = false;
            }
            RenderGridView.ExportCvs(gridExport, table);
           
        }

        protected void btnGroup_Click(object sender, EventArgs e)
        {
            RenderGridView.GridFilter(grid, btnGroup, "显示拖动分组", "取消显示分组", "group");
            LoadData();
        }
        protected void btnHead_Click(object sender, EventArgs e)
        {
            //RenderGridView.WhereFilds = RenderGridView.GridFilter(grid, btnHead, "多字段查询", "取消多字段查询", "filter");
            LoadData();
        }
        protected void pager_PageIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_BeforeColumnSortingGrouping(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewBeforeColumnGroupingSortingEventArgs e)
        {
            RenderGridView.ColumnSortingGrouping(e);
            LoadData();
        }

        /// <summary>
        /// 多字段查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grid_ProcessColumnAutoFilter(object sender, ASPxGridViewAutoFilterEventArgs e)
        {
            RenderGridView.ColumnAutoFilter(grid, e);
            LoadData();
        }

        protected void grid_CustomColumnDisplayText(object sender, ASPxGridViewColumnDisplayTextEventArgs e)
        {
            RenderGridView.ConvertEnum(e);
        }

    }
}